/**
 *  Project: CloudstickUtils 
 *  kr.cloudstick.util-CSZip.java
 * 	
 *	Copyright 2013 Cloudstick (www.cloudstick.kr)
 *
 *	Written by htkim
 *		2013. 3. 26. first written.
 *
 *	Note:	
 */
package kr.cloudstick.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Stack;

import lombok.Cleanup;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;

public final class CSZip { // NOPMD by cloudstick on 13. 3. 28 5:07
	
	private static final int BUFFER_LENGTH = 1024;
	
	private CSZip() {
		throw new AssertionError();
	}
	
	public static byte[] getEntry(final String src, final String entryName) {
		return getEntry(new File(src), entryName);
	}
	
	public static byte[] getEntry(final File src, final String entryName) { // NOPMD by cloudstick on 13. 3. 28 5:04
		if ( src == null || !src.exists() )
		{
			throw new CSException("Invalid source parameter.");
		}
		if ( entryName == null || "".equals(entryName) )
		{
			throw new CSException("Invalid entry name.");
		}
		
		try {
			@Cleanup final ZipFile zipFile = new ZipFile(src, Charset.defaultCharset().name());
			
			final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
			ZipArchiveEntry entry = null; //NOPMD
			boolean foundEntry = false;	//NOPMD
			while( entries.hasMoreElements() ) {
				entry = entries.nextElement();
				if ( !entry.getName().equals(entryName) ) { continue; }
				
				foundEntry = true;
				break;
			}
			
			if ( !foundEntry ) { return null; } // NOPMD by cloudstick on 13. 3. 28 5:05
			
			final InputStream inputStream = zipFile.getInputStream(entry);
			@Cleanup final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			final byte[] buf = new byte[BUFFER_LENGTH];
			
			int ret = inputStream.read(buf);
			while ( ret != -1 ) {
				outputStream.write(buf, 0, ret);
				ret = inputStream.read(buf);
			}
			
			return outputStream.toByteArray();
			
		} catch(IOException e) {
			throw new CSException("Error while creating the ZipFile[" + src.getAbsolutePath() + "]", e);
		}
	}
	
	public static void unzip(final String src, final String dst) {
		
	}
	
	public static void unzip(final File src, final String dst) {
		
	}

	public static void zip(File src) {
		
	}
	
	/**
	 * zip archive src to dst
	 * @param src target file or directory path
	 * @param dst destination file path
	 */
	public static void zip(final String src, final String dst) {
		
	}
	
	public static void zip(final File src, final String dst) {
		
	}
	
	public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) {
		
	}
	
	private static String toPath(File root, File dir) {
		String path = dir.getAbsolutePath();
		path = path.substring(root.getAbsolutePath().length()).replace(File.separatorChar, '/');
		if ( path.startsWith("/")) path = path.substring(1);
		if ( dir.isDirectory() && !path.endsWith("/")) path += "/" ;
		return path ;
	}
}
